package com.linlang.grasharepro.activity.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.ViewFlipper;

import com.linlang.grasharepro.R;
import com.linlang.grasharepro.activity.ADBanner.BaseWebActivity;
import com.linlang.grasharepro.activity.ShareDetailsActivity;
import com.linlang.grasharepro.adapter.BiYeShareAdapter;
import com.linlang.grasharepro.adapter.ImagePagerAdapter;
import com.linlang.grasharepro.common.ViewFlow;
import com.linlang.grasharepro.dao.ShareCoentenDB;
import com.linlang.grasharepro.model.ShareCoenten;
import com.linlang.grasharepro.utils.ListViewUitls;
import com.linlang.grasharepro.view.CircleFlowIndicator;

import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by lixin on 2016-8-6.
 */
public class MainPage extends Fragment {

    private ViewFlow mViewFlow;
    private ShareCoentenDB shareCoentenDomainDB;
    private CircleFlowIndicator mFlowIndicator;
    private ArrayList<String> imageUrlList = new ArrayList<String>();
    ArrayList<String> linkUrlArray= new ArrayList<String>();
    ArrayList<String> titleList= new ArrayList<String>();
    private int mCurrPos;
    private LayoutInflater inflater;
    private View view;
    private LinearLayout notice_parent_ll;
    private LinearLayout notice_ll;
    private ViewFlipper notice_vf;
    //分享的LISTVIEW
    private ListView shareListView;
    //分享的LIST变量

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        this.inflater = inflater;
        view = inflater.inflate(R.layout.mainpage,container,false);
        initView();
        initData();
        initBanner(imageUrlList);
        initRollNotice();
        return view;
    }

    private void initData(){
        shareCoentenDomainDB = new ShareCoentenDB(getContext());
        shareCoentenDomainDB.open();
        final ArrayList<ShareCoenten> share_list = shareCoentenDomainDB.queryAll();
        final BiYeShareAdapter shareAdapter = new BiYeShareAdapter(share_list,getContext());
        shareListView.setAdapter(shareAdapter);
        ListViewUitls.setListViewHeightBasedOnChildren(shareListView);
        //设置单击事件
        shareListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Bundle bundle = new Bundle();
                bundle.putSerializable("shareContent", share_list.get(position));
                Intent intent = new Intent(getContext(), ShareDetailsActivity.class);
                intent.putExtras(bundle);
                getContext().startActivity(intent);
            }
        });
    }

    private void initView(){
        mViewFlow = (ViewFlow)view.findViewById(R.id.viewflow);
        mFlowIndicator = (CircleFlowIndicator)view.findViewById(R.id.viewflowindic);
        shareListView = (ListView) view.findViewById(R.id.shareList);
        imageUrlList.clear();
        linkUrlArray.clear();
        titleList.clear();
        imageUrlList .add(R.drawable.baidu+"");
        imageUrlList .add(R.drawable.sina+"");
        imageUrlList .add(R.drawable.taobao+"");

        linkUrlArray
                .add("https://www.baidu.com");
        linkUrlArray
                .add("http://www.sina.com");
        linkUrlArray
                .add("http://www.taobao.com");
        titleList.add("百度搜索引擎");
        titleList.add("新浪新闻与微博");
        titleList.add("淘宝网，淘啊淘 ");
    }

    private void initRollNotice() {
        FrameLayout main_notice = (FrameLayout)view.findViewById(R.id.main_notice);
        notice_parent_ll = (LinearLayout)inflater.inflate(
                R.layout.layout_notice, null);
        notice_ll = ((LinearLayout) this.notice_parent_ll
                .findViewById(R.id.homepage_notice_ll));
        notice_vf = ((ViewFlipper) this.notice_parent_ll
                .findViewById(R.id.homepage_notice_vf));
        main_notice.addView(notice_parent_ll);
        TimerTask task = new TimerTask() {
            @Override
            public void run() {
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        moveNext();
                    }
                });

            }
        };
        Timer timer = new Timer();
        timer.schedule(task, 0, 4000);
    }

    private void moveNext() {
        setView(this.mCurrPos, this.mCurrPos + 1);
        this.notice_vf.setInAnimation(getContext(), R.anim.in_bottomtop);
        this.notice_vf.setOutAnimation(getContext(), R.anim.out_bottomtop);
        this.notice_vf.showNext();
    }

    private void setView(int curr, int next) {
        View noticeView = inflater.inflate(R.layout.notice_item,
                null);
        TextView notice_tv = (TextView) noticeView.findViewById(R.id.notice_tv);
        if ((curr < next) && (next > (titleList.size() - 1))) {
            next = 0;
        } else if ((curr > next) && (next < 0)) {
            next = titleList.size() - 1;
        }
        notice_tv.setText(titleList.get(next));
        notice_tv.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                Bundle bundle = new Bundle();
                bundle.putString("url", linkUrlArray.get(mCurrPos));
                bundle.putString("title", titleList.get(mCurrPos));
                Intent intent = new Intent(getActivity(),
                        BaseWebActivity.class);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
        if (notice_vf.getChildCount() > 1) {
            notice_vf.removeViewAt(0);
        }
        notice_vf.addView(noticeView, notice_vf.getChildCount());
        mCurrPos = next;

    }
    private void initBanner(ArrayList<String> imageUrlList) {

        mViewFlow.setAdapter(new ImagePagerAdapter(getContext(), imageUrlList,
                linkUrlArray, titleList).setInfiniteLoop(true));
        mViewFlow.setmSideBuffer(imageUrlList.size()); // 实际图片张数，
        // 我的ImageAdapter实际图片张数为3

        mViewFlow.setFlowIndicator(mFlowIndicator);
        mViewFlow.setTimeSpan(4000);
        mViewFlow.setSelection(imageUrlList.size() * 1000); // 设置初始位置
        mViewFlow.startAutoFlowTimer(); // 启动自动播放
    }
}
